/*
 * Created by JFormDesigner on Thu Jun 08 17:36:32 CST 2023
 */

package gui.user;

import dao.SaleDao;
import dao.SaleDetailsDao;
import entity.Sale;
import entity.SaleDetails;
import gui.user.model.SaleDetailsTableModel;
import gui.user.model.UserOrderTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class MyOrderFrame extends JFrame {
    public MyOrderFrame(String userId) throws Exception {
        initComponents(userId);
    }

    private void initComponents(String userId) throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();
        label2 = new JLabel();
        scrollPane2 = new JScrollPane();
        table2 = new JTable();
        refundBtn = new JButton();
        cancelBtn = new JButton();

        //======== this ========
        setTitle("\u6211\u7684\u8ba2\u5355");
        setPreferredSize(new Dimension(920, 560));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
            table1.setModel(new UserOrderTableModel(userId));
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(40, 65, 400, 360);

        //给表格添加鼠标点击事件
        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                String saleId = (String) table1.getValueAt(row,0);//获取销售单编号
                try {
                    table2.setModel(new SaleDetailsTableModel(saleId));
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        //---- label1 ----
        label1.setText("\u8ba2\u5355");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(220, 20, 120, 30);

        //---- label2 ----
        label2.setText("\u8ba2\u5355\u8be6\u7ec6");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(635, 20, 120, 30);

        //======== scrollPane2 ========
        {

            //---- table2 ----
            table2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
            scrollPane2.setViewportView(table2);
        }
        contentPane.add(scrollPane2);
        scrollPane2.setBounds(465, 65, 400, 360);

        //---- refundBtn ----
        refundBtn.setText("\u9000\u6b3e");
        refundBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(refundBtn);
        refundBtn.setBounds(345, 460, 95, 45);

        refundBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                String saleId = (String) table1.getValueAt(row,0);//获取销售单编号

                SaleDao saleDao = new SaleDao();
                try {
                    Sale sale = saleDao.getAppointSale2(saleId);
                    SaleDetailsDao saleDetailsDao = new SaleDetailsDao();
                    try {
                        List saleDetailsList = saleDetailsDao.getAppointSaleDeatils(saleId);
                        for (int i = 0; i < saleDetailsList.size(); i++) {
                            SaleDetails saleDetails = (SaleDetails) saleDetailsList.get(i);
                            saleDetailsDao.deleteSaleDetails(saleDetails);
                        }
                        saleDao.deleteSale(sale);
                        table1.setModel(new UserOrderTableModel(userId));
                        JOptionPane.showMessageDialog(null,"退款成功！");
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //---- cancelBtn ----
        cancelBtn.setText("\u8fd4\u56de");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(465, 460, 95, 45);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                UserMainFrame userMainFrame = new UserMainFrame(userId);
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label1;
    private JLabel label2;
    private JScrollPane scrollPane2;
    private JTable table2;
    private JButton refundBtn;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        MyOrderFrame myOrderFrame = new MyOrderFrame("111");
    }
}
